package com.shengqiang.cloud.ups.service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.shengqiang.cloud.ups.common.model.JsonResult;
import com.shengqiang.cloud.ups.dao.modal.UpmsDepartment;
import com.shengqiang.cloud.ups.dao.repository.master.UpmsDepartmentMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/2/21.
 */
@Service
public class UpmsDepartmentWebService extends ServiceImpl<UpmsDepartmentMapper,UpmsDepartment>{

    private static final Logger logger= LoggerFactory.getLogger(UpmsDepartmentWebService.class);

    public JsonResult<List<UpmsDepartment>> selectDepartments(Map<String,Object> params){
        JsonResult<List<UpmsDepartment>> result=new JsonResult<>(1,"fail");
        try{
            List<UpmsDepartment> departments=baseMapper.selectDepartments(params);
            if(departments!=null){
                result.setData(departments);
                result.setCode(0);
                result.setMsg("success");
            }else {
                result.setCode(1);
                result.setMsg("部门列表为空");
            }
        }catch (Exception ex){
            logger.error("查询部门信息出现异常:{}",ex.getMessage(),ex);
        }
        return result;
    }

    public JsonResult addDeparatment(UpmsDepartment department){
        JsonResult result=new JsonResult(1,"添加失败");
        try{
            int addRow=baseMapper.insert(department);
            if(addRow>0){
                result.setCode(0);
                result.setMsg("添加部门信息成功");
            }
        }catch (Exception ex){
            logger.error("添加部门信息出现异常:{}",ex.getMessage(),ex);
        }
        return result;
    }

    public JsonResult updateDepartment(UpmsDepartment department){
        JsonResult result=new JsonResult(1,"修改部门信息失败");
        try{
            int rowCount=baseMapper.updateById(department);
            if(rowCount>0){
                result.setCode(0);
                result.setMsg("修改部门信息成功");
            }
        }catch (Exception ex){
            logger.error("修改部门信息出现异常:{}",ex.getMessage(),ex);
        }

        return result;
    }

    public JsonResult deleteDepartment(List<Integer> ids){
        JsonResult result=new JsonResult(1,"删除失败");
        try{
            int deleteRow=baseMapper.deleteBatchIds(ids);
            if(deleteRow>0){
                result.setCode(0);
                result.setMsg("删除成功");
            }
        }catch (Exception ex){
            logger.error("删除部门信息出现异常:{}",ex.getMessage(),ex);
        }
        return result;
    }
}
